package com.example.springtxjta.web;

import com.alibaba.fastjson.JSON;
import com.example.springtxjta.domain.Customer;
import com.example.springtxjta.service.CustomerService;
import com.example.springtxjta.service.CustomerServiceInCode;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/customer")
public class CustomerResource {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerServiceInCode customerServiceInCode;
    @Autowired
    private JmsTemplate jmsTemplate;

    @PostMapping("/code")
    public Customer createInCode(@RequestBody Customer customer) {
        return customerServiceInCode.create(customer);
    }

    @Transactional
    @PostMapping("/annotation")
    public Customer create(@RequestBody Customer customer) {
        return customerService.create(customer);
    }

    @Transactional
    @PostMapping("/listener")
    public void createByListener(@RequestBody Customer customer) {
        jmsTemplate.convertAndSend(CustomerService.NEW_MSG_KEY, JSON.toJSONString(customer));
    }

    @GetMapping("/all")
    public List<Customer> getAll() {
        return customerService.getAll();
    }

}
